﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ToolHelper.DataGridViewExtern;

namespace CommonLibrary.CustControls
{
    public partial class FrmAddData : Form
    {

        public FrmAddData(DataGridView grdView)
        {
            InitializeComponent();
            CopyGrdViewData.InitData(ref this.GrdBindingData, grdView);
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void BtnDone_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        public DataGridView GetDataGridView()
        {
            return this.GrdBindingData;
        }

        //private void AddCols(DataGridViewColumnCollection columns)
        //{
        //    if (columns == null)
        //        return;
        //    List<DataGridViewColumn> LstGrdCol = new List<DataGridViewColumn>();
        //    Int32 colIndex = 0;
        //    foreach (ExternPropTextBoxColumn col in columns)
        //    {
        //        var newCol = Activator.CreateInstance(typeof(ExternPropTextBoxColumn)) as ExternPropTextBoxColumn;
        //        if (newCol != null)
        //        {
        //            newCol.DisplayIndex = colIndex;
        //            newCol.HeaderText = col.HeaderText;
        //            newCol.Name = col.Name;
        //            newCol.Fillable = col.Fillable;
        //            LstGrdCol.Add(newCol);
        //            colIndex++;
        //        }
        //    }
        //    this.GrdBindingData.Columns.AddRange(LstGrdCol.ToArray());
        //    LstGrdCol.Clear();
        //}

        //private void InitData(DataGridView grdView)
        //{
        //    var colCount = grdView.Columns.Count;
        //    DataGridViewColumn[] columns = new DataGridViewColumn[colCount];
        //    grdView.Columns.CopyTo(columns, 0);
        //    Int32 colIndex = 0;

        //    this.GrdBindingData.Columns.AddRange(columns);

        //    //var rowCount = grdView.Rows.Count;
        //    //DataGridViewRow[] rows = new DataGridViewRow[rowCount];
        //    //grdView.Rows.CopyTo(rows, 0);
        //    //this.GrdBindingData.Rows.Add(rowCount);
        //    //for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
        //    //{

        //    //}
        //}

        //private DataTable produceDT()
        //{
        //    DataTable dt = new DataTable();
        //    this.createDThead(ref dt);
        //    this.createDTbody(ref dt);
        //    return dt;
        //}

        //private void createDThead(ref DataTable dt)
        //{
        //    var cols = this.GrdBindingData.Columns;
        //    foreach (DataGridViewColumn col in cols)
        //    {
        //        DataColumn newCol = new DataColumn
        //        {
        //            Caption = col.HeaderText,
        //            ColumnName = col.Name,
        //            DataType = typeof(String)
        //        };
        //        dt.Columns.Add(newCol);
        //    }
        //}

        //private void createDTbody(ref DataTable dt)
        //{
        //    var rows = this.GrdBindingData.Rows;
        //    var rowCount = rows.Count - 1;
        //    var colCount = this.GrdBindingData.Columns.Count;
        //    for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
        //    {
        //        var newRow = dt.NewRow();
        //        for (int colIndex = 0; colIndex < colCount; colIndex++)
        //            newRow[colIndex] = rows[rowIndex].Cells[colIndex].Value;
        //        dt.Rows.Add(newRow);
        //    }
        //}
    }
}
